<template>
  <div class="app-container">
    <div class="search-box">
      <div class="search-hd">系统监控任务</div>
    </div>
    <div class="main-box">
      <div class="main-hd">系统监控任务</div>
      <el-table
        v-loading="loading"
        :data="list"
        stripe
        max-height="500"
        fit
        highlight-current-row
      >
        <el-table-column label="序号" width="95" align="center">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column
          label="任务分组"
          prop="jobGroup"
          align="center"
        ></el-table-column>
        <el-table-column
          label="任务名"
          prop="jobName"
          align="center"
        ></el-table-column>
        <el-table-column
          label="上次触发时间"
          prop="previousFireTime"
          align="center"
        ></el-table-column>
        <el-table-column
          label="下次触发时间"
          prop="nextFireTime"
          align="center"
        ></el-table-column>
        <el-table-column
          label="间隔时间（小时）"
          prop="repeatInterval"
          align="center"
        ></el-table-column>
           <el-table-column label="修改间隔时间" align="center">
         <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="handle3(scope.row)"
              >修改</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.triggerState"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="ignore(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
      </el-table>
    </div>
      <el-dialog title="修改间隔时间" :visible.sync="dialogFormVisible">
          <el-form :model="logForm">
            <el-form-item label="间隔时间（小时）" :label-width="formLabelWidth">
              <el-input v-model="logForm.htime" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="handle2(logForm)"
              >确 定</el-button
            >
          </div>
        </el-dialog>
  </div>
</template>

<script>
import { quartzselect, quartzpause, quartzresume,sheduletrigger } from "@/api/table";

export default {
  data() {
    return {
      logForm: {
        id: "",
        htime: "",
      },
      list: null,
      loading: true,
      total: 10,
      searchField: {
        ruleName: "",
        ruleNum: "",
        wfAppid: "",
        page: 1,
        limit: 15,
      },
      checkedId: "",
      checkList: [],
      dialogVisible: false,
      dialogFormVisible: false,
      dialogUpdateFormVisible: false,
      formLabelWidth: "150px",
    };
  },
  created() {
    this.initListData();
  },
  methods: {
     // 处置
    handle3(row) {
      console.log(row);
      this.dialogFormVisible = true;
      this.logForm.id = row.jobName;
      this.logForm.isignore = 2;
    },
      // 修改间隔时间
    handle2(logForm) {
      this.dialogFormVisible = false;
      logForm.htime=logForm.htime*60*60
      console.log(logForm);
      // 向后台发送请求
      sheduletrigger(logForm).then(res=>{
          var flag = res.flag;
          if (flag) {
            this.$message.success("处置成功");
            this.logForm = {
              id: "",
              htime: "",
              hcontact: "",
              hcontent: "",
              isignore: "",
            };
            // 重新查询日志
            setTimeout(()=>{
            this.initListData();
            },1000)
          } else {
            this.$message.error("处置失败");
          }
      })
    },

    // 关闭
    ignore(row) {
      console.log(row);
      if (!row.triggerState) {
        this.open(row);
        return;
      }
      this.handle(row);
    },
    // 开启
    handle(row) {
      console.log(row);
      quartzresume(row.jobName).then((res) => {
        if (res.flag == true) {
          this.$message.success("任务已开启");
          this.initListData();
        } else {
          this.$message.error("任务已开启已取消");
        }
      });
    },
    timestampToTime(timestamp) {
      var strDate = "";
      var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      console.log(date);
      var Y = date.getFullYear() + "-";
      var M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var D =
        (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
      var h =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      var m =
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        ":";
      var s =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      strDate = Y + M + D + h + m + s;
      console.log(strDate);
      return strDate;
    },
    initListData() {
      this.loading = true;
      quartzselect(this.searchField).then((res) => {
        let arr = res.data.map(item=>{
          item.trigger.triggerState=item.triggerState
          return item.trigger
        });
        console.log(arr);
        arr.map((item) => {
          item.nextFireTime = this.timestampToTime(item.nextFireTime);
          item.previousFireTime = this.timestampToTime(item.previousFireTime);
          item.repeatInterval = (item.repeatInterval/1000 / 3600).toFixed(2);
        });
        console.log(arr);
        this.list = arr;
        this.loading = false;
      });
    },
    // 执行关闭
    updateLog(log) {
      console.log(log);
      quartzpause(log.jobName).then((res) => {
        if (res.flag == true) {
          this.$message.success("任务已关闭");
          this.initListData();
        } else {
          this.$message.error("任务已关闭已取消");
        }
      });
    },
    // 消息提示框信息
    open(row) {
      this.$confirm("此操作将会关闭该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.updateLog(row);
        })
        .catch(() => {
          row.triggerState=true
          this.$message({
            type: "info",
            message: "已取消忽略",
          });
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.redColor {
  color: red;
}
.search-box {
  margin-bottom: 15px;
  .search-hd {
    border-left: 2px solid #00a73c;
    padding: 10px;
    margin-bottom: 10px;
  }
  .search-bd {
    .el-button--small {
      margin-top: 3px;
    }
  }
}
.main-box {
  .main-hd {
    height: 40px;
    line-height: 40px;
    background: #00a73c;
    color: #fff;
    padding-left: 10px;
  }
  .page-box {
    padding: 10px;
    text-align: center;
  }
}
</style>
